<template>
    <div class="ele-body">
      <!-- 搜索表单 -->
      <paycustomeraccountSearch @search="reload"  />
      <!-- 数据表格 -->
      <el-card shadow="never">
        <ele-pro-table
          ref="table"  row-key="id" :columns="columns" :datasource="datasource"  :need-page="true"   @done="onDone"    :height="tableHeight" :full-height="tableFullHeight"
        >
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <el-button
            v-permission="'Financial_Paycustomeraccount_Export'"
              size="small"
              icon="el-icon-upload2"
              class="ele-btn-icon"
              @click="reportexcel"
            >
              导出
            </el-button>
          </template>
          <template slot="billNo" slot-scope="{ row }"  >
            <el-link  type="primary"   @click="clickbillNo(row)" >{{ row.billNo }}</el-link>
          </template>
           <!-- 操作列 -->
         <template slot="action" slot-scope="{ row }"  >
          <el-link type="info"  :underline="false"  icon="el-icon-menu" @click="openListEdit(row,'2')" >明细</el-link>
          <el-link type="success" v-if="row.paySurplusAmount>0" :underline="false"  icon="el-icon-check" @click="openEdit(row)" v-permission="'Financial_Paycustomeraccount_SK'">付款</el-link>
        </template>
        </ele-pro-table>
      </el-card>
      <!-- 编辑弹窗 -->
      <paycustomeraccountEdit :data="current" :visible.sync="showEdit" @done="reload" />

       <!-- 编辑弹窗 -->
      <paycustomeraccountitemList :data="current" :visible.sync="showListEdit" @done="reload" />
      <!-- 导出数据 -->
      <ExportDialog ref="ExportDialog"></ExportDialog>
    </div>
  </template>
  
  <script>
    import { removePageTab } from '@/utils/page-tab-util';
    import ExportDialog from '@/components/Excel/ExportDialog'
    import paycustomeraccountEdit from './components/paycustomeraccount-edit.vue';
    import paycustomeraccountSearch from './components/paycustomeraccount-search.vue';
    import paycustomeraccountitemList from './components/paycustomeraccountitem-list.vue';
    import { QueryRecaccountauitByCondition } from '@/api/finance/recaccountauit';

    export default {
      name: 'Salecommission',
      components: {paycustomeraccountSearch,paycustomeraccountEdit,paycustomeraccountitemList,ExportDialog},
      data() {
        return {
          // 表格列配置
          columns: [
            {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
            {prop: 'orderNo', label: '单据编号',  width: 150,showOverflowTooltip: true,align: 'center'},
            {prop: 'auitOrderTypeDesc', label: '单据类型',  width: 150,showOverflowTooltip: true,align: 'center'},
            {prop: 'customerName', label: '客户名称',  width: 120,showOverflowTooltip: true,align: 'center',sortable:'customerId'},
            {prop: 'payAmount', label: '应付金额', width: 150, width: 150, showOverflowTooltip: true,align: 'center',sortable:'payAmount'},
            {prop: 'payActAmount', label: '已付金额', width: 150, width: 150, showOverflowTooltip: true,align: 'center',sortable:'payActAmount'},
            {prop: 'paySurplusAmount', label: '未付金额', width: 150, showOverflowTooltip: true,align: 'center',sortable:'paySurplusAmount'},
            {prop: 'currencyDesc', label: '币种', showOverflowTooltip: true,align: 'center',sortable:'currencyId'},
            {prop: 'rate', label: '汇率', showOverflowTooltip: true,align: 'center',sortable:'rate'},
            {prop: 'payAmountCn', label: '应付金额(￥)', width: 150, width: 150, showOverflowTooltip: true,align: 'center',sortable:'payAmountCn'},
            {prop: 'payActAmountCn', label: '已付金额(￥)', width: 150, showOverflowTooltip: true,align: 'center',sortable:'payActAmountCn'},
            {prop: 'paySurplusAmountCn', label: '未付金额(￥)', width: 150, showOverflowTooltip: true,align: 'center',sortable:'paySurplusAmountCn'},
            {
              prop: 'orderDate', label: '单据日期', showOverflowTooltip: true, align: 'center',sortable: 'orderDate',width: 150, 
              formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
            },
            {columnKey: 'action', label: '操作',width: 150,align: 'center',slot:'action', resizable: false, hideInSetting: true,fixed: 'right',  showOverflowTooltip: true}
          ],
          //是否详情
          fixedHeight:true,
          // 当前编辑数据
          current: null,
          // 是否显示编辑弹窗
          showEdit: false,
          // 是否显示编辑弹窗
          showListEdit: false,
          // 导出列
          exportList: [
            { name: "单据编号", keys: "orderNo", status: true },
            { name: "单据类型", keys: "auitOrderTypeDesc", status: true },
            { name: "客户名称", keys: "customerName", status: true },
            { name: "应付金额", keys: "payAmount", status: true },
            { name: "已付金额", keys: "payActAmount", status: true },
            { name: "未付金额", keys: "paySurplusAmount", status: true },
            { name: "币种", keys: "currencyDesc", status: true },
            { name: "汇率", keys: "rate", status: true },
            { name: "应付金额(￥)", keys: "payAmountCn", status: true },
            { name: "已付金额(￥)", keys: "payActAmountCn", status: true },
            { name: "未付金额(￥)", keys: "paySurplusAmountCn", status: true },
            { name: "单据日期", keys: "orderDate", status: true },
          ]
        };
      },
      computed: {
        // 表格高度
        tableHeight() {
          var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;

          return fh;
        },
        // 表格全屏时高度
        tableFullHeight() {
          var fh =  this.fixedHeight ? 'calc(100vh - 126px)' : null;

          return fh;
        },
      },
      methods: {
        /* 表格数据源 */
        async datasource(params) {
          params.where.pageindex = params.page;
          params.where.pagesize = params.limit;
  
          //添加排序
          if(params.order.order == null || params.order.order == undefined){
            params.where.orderBys = [{ sort: "createDate", order: 1, }];
          }else{
            params.where.orderBys = [];
            params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
          }

          if(!params.where.conditions)params.where.conditions= [];

          /* 应付金额大于零 */
          params.where.conditions.push({key:"payAmount",operator: 2,value:  0});
          let data = await QueryRecaccountauitByCondition(params.where);

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          return {
              list:data.items,
              count:data.totalrecords,
          };
        },
  
        /* 表格渲染完成回调 */
        onDone({ data }) {
          this.supplierList = data;
        },

        /* 刷新表格 */
        reload(where) {
          
          this.$refs.table.reload({ where: where });
        },
        
        //单击单号
        clickbillNo(row){
          removePageTab({ key: '/businesshandling/saleoutmanger' });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/saleoutmanger', query: { "orderNo": row.billNo } });
          });
        },
        /**
         * 显示编辑
         * @param row 当前行对像
         */
        openEdit(row) {
          this.current = row;
          this.showEdit = true;
        },

         /**
         * 显示编辑
         * @param row 当前行对像
         */
         openListEdit(row,index) {
          this.current = row;
          this.showListEdit = true;
          // removePageTab({ key: '/financialmanager/salecommission/salecommissiondetail' });
          // this.$nextTick(() => {
          //   this.$router.push({path: '/financialmanager/salecommission/salecommissiondetail', query: { 'salecommission':JSON.stringify(row)} });
          // });
        },

        /* 删除 */
        remove(row) {
          const loading = this.$loading({ lock: true });
          DeleteIncome(row.id)
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        },

        /* 导出 */
        reportexcel(pams) {

          //初始化参数
          let params = {
            pagesize: 0,
            pageindex: 1,
            conditions: [],
            orderBys: [{ sort: "createDate", order: 1, }],
          };
          //加载进度条
          const loading = this.$loading({ lock: true });
          //重新请求查询
          this.$refs.table.doRequest(({ where }) => {
          if(where){
            params.orderBys = where.orderBys;
            params.pageindex = where.pageindex;
            params.conditions = where.conditions;
          }
          //查询数据
          QueryRecaccountauitByCondition(params)
            .then((data) => {
              loading.close();
              this.$refs.ExportDialog.showEdits = true
              this.$refs.ExportDialog.checkList = this.exportList
              this.$refs.ExportDialog.titleName = "客户应付账款列表"
              this.$refs.ExportDialog.dataList = data.items;
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
          });
        },
      }
    };
  </script>./components/paycustomeraccount-search.vue./components/paycustomeraccount-edit.vue